from functools import partial


def merge_sort(nums):
    if len(nums)<=1:
        return nums
    return op_with_rp(merge_sort(nums[:len(nums)//2]), merge_sort(nums[len(nums)//2:]))

def merge_two(l, r, op):
    op(l, r)
    return sorted(l+r)

class ReversePair:
    def __init__(self):
        self.cnt = 0

    def __call__(self, *args, **kwargs):
        l, r = args
        i, j = 0, 0
        while i < len(l) and j<len(r):
            if l[i] > 2 * r[j]:
                self.cnt += len(l) - i
                j += 1
            else:
                i += 1

rp = ReversePair()
op_with_rp = partial(merge_two, op=rp)
merge_sort([1,3,2,3,1])
print(rp.cnt)